package com.dxt.dao;

import com.dxt.pojo.Delivery;
import com.dxt.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * BelongProject: deliveryManage
 * BelongPackage: com.dxt.dao
 * Description:
 *
 * @Author 段鑫涛
 * @Create 2025/3/30 23:41
 * @Version 1.0
 */
public class DeliveryDaoImpl implements DeliveryDao {
    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
    @Override
    public List<Delivery> getDeliveryListByUserId(Integer id) throws SQLException {
        String sql = "select id,delivery_name as deliveryName,user_id as userId,company_id as companyId,phone,address,send_time as sendTime,state from sys_delivery where user_id = ?";
        return queryRunner.query(sql, new BeanListHandler<>(Delivery.class), id);
    }

    @Override
    public void addDeliveryByUserId(String deliveryName, Integer id, String companyId, String phone, String address, String sendTime, String state) throws SQLException {
        String sql ="insert into sys_delivery values(null,?,?,?,?,?,?,?)";
        queryRunner.update(sql,deliveryName,id,companyId,phone,address,sendTime,state);
    }

    @Override
    public void deleteDeliveryById(String id) throws SQLException {
        String sql = "delete from sys_delivery where id = ?";
        queryRunner.update(sql,id);
    }

    @Override
    public Delivery getDeliveryListByDeliveryId(String id) throws SQLException {
        String sql = """
                SELECT id,delivery_name as deliveryName,user_id as userId, 
                company_id as companyId, phone, address, 
                send_time as sendTime, state from sys_delivery
                WHERE id = ?
                """;
        return queryRunner.query(sql, new BeanHandler<Delivery>(Delivery.class), id);
    }

    @Override
    public void updateDeliveryByDeliveryId(Map<String, String> map, Integer userId) throws SQLException {
        String sql = """
                    update sys_delivery set 
                        delivery_name = ?, 
                        user_id = ?,
                        company_id = ?,
                        phone = ?,
                        address = ?,
                        send_time = ?,
                        state = ?
                        where id = ?
                    """ ;
        queryRunner.update(sql,
                map.get("delivery_name"),
                userId,
                map.get("company_id"),
                map.get("phone"),
                map.get("address"),
                map.get("send_time"),
                map.get("state"),
                map.get("id"));
    }
}
